/******************************************************************************
 * TODO: 
 * 		 Thread to update claw button and debug window and display crash message
 *       Sending new robot state to server (check the current state and then
 *       sending the new desired state if different)
 *       Re-arrange buttons for better layout
 *       complete information in about box
 *****************************************************************************/

import java.awt.Color;
import java.awt.Panel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.InetAddress;

import javax.media.protocol.DataSource;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import common.RobotInformation;
import common.Shared;


public class Client extends Panel  implements MouseListener, Runnable{

	private static final long serialVersionUID = 6463534731269380864L;

	private static final int DEFAULT_PORT_NUMBER = 9068;
	
	public boolean notbusy = false;
	//
	public static FTP_Client ftpClient;
	public static FTP_Server ftpServer;
	public static JFrame mainframe;
	public static DataSource vidDataSource;
	public static DebugWindow dWindow;
	//
	public static ClientInfo cInfo;
	//Buttons
	public static mediapanel vpanel; //the video 
	public static JLabel bgimage; //the background image
	public static JButton exitButton; //the exit button
	public static JButton aboutButton; //the about button
	public static JButton forwardButton; //the forward button
	public static JButton backwardButton; //the backward button
	public static JButton leftButton; //the turn left button
	public static JButton rightButton; //the turn right button
	public static JButton clawButton; //the open/close claw button
	public static JLabel connlostLabel; //the connection lost panel
	
	//images
	public static ImageIcon bgicon;
	public static ImageIcon exiticon_up;
	public static ImageIcon exiticon_down;
	public static ImageIcon abouticon_up;
	public static ImageIcon abouticon_down;
	public static ImageIcon forwardicon_up;
	public static ImageIcon forwardicon_down;
	public static ImageIcon backwardicon_up;
	public static ImageIcon backwardicon_down;
	public static ImageIcon lefticon_up;
	public static ImageIcon lefticon_down;
	public static ImageIcon righticon_up;
	public static ImageIcon righticon_down;
	public static ImageIcon clawopenicon_up;
	public static ImageIcon clawopenicon_down;
	public static ImageIcon clawcloseicon_up;
	public static ImageIcon clawcloseicon_down;
	public static ImageIcon connlosticon;
	
	//server's ip address
	public static InetAddress servIP;
	
	
	public Client(String mediaURL)
	 {
		vpanel = new mediapanel(mediaURL);
		
		//create our icons
		 bgicon = new ImageIcon("images/bg.jpg");
		 exiticon_up = new ImageIcon("images/exit_button_up.jpg");
		 exiticon_down = new ImageIcon("images/exit_button_down.jpg");
		 abouticon_up = new ImageIcon("images/about_button_up.jpg");
		 abouticon_down = new ImageIcon("images/about_button_down.jpg");
		 forwardicon_up = new ImageIcon("images/forward_button_up.jpg");
		 forwardicon_down = new ImageIcon("images/forward_button_down.jpg");
		 backwardicon_up = new ImageIcon("images/backward_button_up.jpg");
		 backwardicon_down = new ImageIcon("images/backward_button_down.jpg");
		 lefticon_up = new ImageIcon("images/turnleft_button_up.jpg");
		 lefticon_down = new ImageIcon("images/turnleft_button_down.jpg");
		 righticon_up = new ImageIcon("images/turnright_button_up.jpg");
		 righticon_down = new ImageIcon("images/turnright_button_down.jpg");
		 clawopenicon_up = new ImageIcon("images/open_button_up.jpg");
		 clawopenicon_down = new ImageIcon("images/open_button_down.jpg");
		 clawcloseicon_up = new ImageIcon("images/close_button_up.jpg");
		 clawcloseicon_down = new ImageIcon("images/close_button_down.jpg");
		 connlosticon = new ImageIcon("images/connection_lost.jpg");
		 
		 //set up the absolute layout
		 setLayout(null);
		 
		 //create our labels "buttons"
		 bgimage = new JLabel();
		 exitButton = new JButton();
		 aboutButton = new JButton();
		 forwardButton = new JButton();
		 backwardButton = new JButton();
		 leftButton = new JButton();
		 rightButton = new JButton();
		 clawButton = new JButton();
		 connlostLabel = new JLabel();
		
		 //tell the buttons and label to use the icons
		 exitButton.setIcon(exiticon_up);
		 exitButton.setPressedIcon(exiticon_down);
		 exitButton.setBorderPainted(false);
		 aboutButton.setIcon(abouticon_up);
		 aboutButton.setPressedIcon(abouticon_down);
		 aboutButton.setBorderPainted(false);
		 bgimage.setIcon(bgicon);
		 forwardButton.setIcon(forwardicon_up);
		 forwardButton.setPressedIcon(forwardicon_down);
		 forwardButton.setBorderPainted(false);
		 backwardButton.setIcon(backwardicon_up);
		 backwardButton.setPressedIcon(backwardicon_down);
		 backwardButton.setBorderPainted(false);
		 leftButton.setIcon(lefticon_up);
		 leftButton.setPressedIcon(lefticon_down);
		 leftButton.setBorderPainted(false);
		 rightButton.setIcon(righticon_up);
		 rightButton.setPressedIcon(righticon_down);
		 rightButton.setBorderPainted(false);
		 clawButton.setIcon(clawcloseicon_up);
		 clawButton.setPressedIcon(clawcloseicon_down);
		 clawButton.setBorderPainted(false);
		 connlostLabel.setIcon(connlosticon);
		 connlostLabel.setVisible(false);
		 
		 //set up our button sizes/location
		 bgimage.setBounds(0, 0, 800, 800);
		 exitButton.setBounds(678,0,120,120);
		 aboutButton.setBounds(0,0,120,120);
		 forwardButton.setBounds(72,580,120,120);
		 backwardButton.setBounds(202,580,120,120);
		 leftButton.setBounds(344,580,120,120);
		 rightButton.setBounds(474,580,120,120);
		 clawButton.setBounds(604,580,120,120);
		 connlostLabel.setBounds(0, 580, 800, 120);
		 //set the video size/location
		 vpanel.setBounds(133, 115, 535, 376);
		 
		 //add the mouselistener to the buttons
		 exitButton.addMouseListener(this);
		 aboutButton.addMouseListener(this);
		 forwardButton.addMouseListener(this);
		 backwardButton.addMouseListener(this);
		 leftButton.addMouseListener(this);
		 rightButton.addMouseListener(this);
		 clawButton.addMouseListener(this);
		 
		 //add our items to the main jframe
		 add(connlostLabel);
		 add(aboutButton);
		 add(exitButton);
		 add(forwardButton);
		 add(backwardButton);
		 add(leftButton);
		 add(rightButton);
		 add(clawButton);
		 add(bgimage);
		 add(vpanel);		 

	 }
	
	 public static void main(String[] args){

		 cInfo = new ClientInfo();
		 
		 mainframe = new JFrame();
		 
		 Ipbox ipb = new Ipbox(mainframe,cInfo);
		 
		 int temp = 0;
		 
		 while(temp == 0){
			 temp = ipb.getCloseStatus();
		 }
		 
		 if(cInfo.d_flag == true)
		 {
			 dWindow = new DebugWindow(mainframe);
			 dWindow.addLine("Debug Window Started...");
		 }
		 //set up sockets and connect to server
		 
		 if(cInfo.d_flag == true)
		 {
			 dWindow.addLine("Connected to server...");
		 }
		 
		 ftpClient = new FTP_Client(DEFAULT_PORT_NUMBER);

		 ftpServer = new FTP_Server(DEFAULT_PORT_NUMBER);
		 Thread td = new Thread(ftpServer);
		 td.start();
		 
		 ftpClient.handleInput("connect " + cInfo.ipstring + " " + "9069");
		 
		// ftpClient.handleInput("query checkconnection");
		 
		 if(cInfo.d_flag == true)
		 {
			 dWindow.addLine("Creating main GUI...");
			 dWindow.addLine("Requesting video feed...");
		 }
		 //this string needs to be replaced with the one received from the main server
		 Client mlr = new Client("rtp://" + cInfo.ipstring + ":9060/video");

		 mainframe.getContentPane().add(mlr);

		 mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		 mainframe.setBackground(Color.BLACK);
		 
		 mainframe.setSize(800,720);
		 
		 mainframe.setResizable(false);		 
		 
		 mainframe.setVisible(true);
		 
		 if(cInfo.d_flag == true)
		 {
			 dWindow.addLine("Main GUI created and displayed...");
		 }
		 
		 Thread listenerThread = new Thread(mlr);
		 listenerThread.start();
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		JButton button = (JButton)arg0.getComponent();
		
		if(button == rightButton)
		{
			//code to send command to server
			notbusy = false;
			 ftpClient.handleInput("query right");
			if(cInfo.d_flag == true)
			{
				dWindow.addLine("Sent Turn Right Command");
			}
			if(Shared.roboInfo.connected == true && Shared.roboInfo.state.equals("right"))
			{
				if(cInfo.d_flag == true)
				{
					dWindow.addLine("Command Turn Right Received Ok");
				}
			}
		}
		else if (button == leftButton)
		{
			//code to send command to server
			notbusy = false;
			 ftpClient.handleInput("query left");
			
			if(cInfo.d_flag == true)
			{
				dWindow.addLine("Sent Turn Left Command");
			}
			if(Shared.roboInfo.connected == true && Shared.roboInfo.state.equals("left"))
			{
				if(cInfo.d_flag == true)
				{
					dWindow.addLine("Command Turn Left Received Ok");
				}
			}
		}
		else if (button == forwardButton)
		{
			//code to send command to server
			notbusy = false;
			 ftpClient.handleInput("query forward");
			
			if(cInfo.d_flag == true)
			{
				dWindow.addLine("Sent Move Forward Command");
			}
			if(Shared.roboInfo.connected == true && Shared.roboInfo.state.equals("forward"))
			{
				if(cInfo.d_flag == true)
				{
					dWindow.addLine("Command Move Forward Received Ok");
				}
			}
		}
		else if (button == backwardButton)
		{
			//code to send command to server
			notbusy = false;
			 ftpClient.handleInput("query backward");
			
			if(cInfo.d_flag == true)
			{
				dWindow.addLine("Sent Move Backward Command");
			}
			if(Shared.roboInfo.connected == true && Shared.roboInfo.state.equals("backward"))
			{
				if(cInfo.d_flag == true)
				{
					dWindow.addLine("Command Move Backward Received Ok");
				}
			}
		}

		else if (button == clawButton)
		{
			if(clawButton.getIcon() == clawopenicon_up)
			{
				//we want to open the claw
				notbusy = false;
				ftpClient.handleInput("query opengrip");
				if(cInfo.d_flag == true)
				{
					dWindow.addLine("Sent Open Claw Command");
				}
				if(Shared.roboInfo.connected == true && Shared.roboInfo.state.equals("opengrip"))
				{
					if(cInfo.d_flag == true)
					{
						dWindow.addLine("Command Open Claw Received Ok");
					}
				}

			}
			else
			{
				//we want to close the claw
				notbusy = false;
				ftpClient.handleInput("query closegrip");
				if(cInfo.d_flag == true)
				{
					dWindow.addLine("Sent Close Claw Command");
				}
				if(Shared.roboInfo.connected == true && Shared.roboInfo.state.equals("closegrip"))
				{
					if(cInfo.d_flag == true)
					{
						dWindow.addLine("Command Close Claw Received Ok");
					}
				}
			}
		}
	}


	@Override
	public void mouseReleased(MouseEvent arg0) {

		JButton button = (JButton)arg0.getComponent();
		
		if(button == rightButton)
		{
			//code to send command to server
			 ftpClient.handleInput("query stop");
			
			if(cInfo.d_flag == true)
			{
				dWindow.addLine("Sent Stop Command");
			}
			if(Shared.roboInfo.connected == true && Shared.roboInfo.state.equals("stop"))
			{
				if(cInfo.d_flag == true)
				{
					dWindow.addLine("Command Stop Received Ok");
				}
			}
			notbusy = true;
		}
		else if (button == leftButton)
		{
			//code to send command to server
			 ftpClient.handleInput("query stop");
			
			if(cInfo.d_flag == true)
			{
				dWindow.addLine("Sent Stop Command");
			}
			if(Shared.roboInfo.connected == true && Shared.roboInfo.state.equals("stop"))
			{
				if(cInfo.d_flag == true)
				{
					dWindow.addLine("Command Stop Received Ok");
				}
			}
			notbusy = true;
		}
		else if (button == forwardButton)
		{
			//code to send command to server
			 ftpClient.handleInput("query stop");
			
			if(cInfo.d_flag == true)
			{
				dWindow.addLine("Sent Stop Command");
			}
			if(Shared.roboInfo.connected == true && Shared.roboInfo.state.equals("stop"))
			{
				if(cInfo.d_flag == true)
				{
					dWindow.addLine("Command Stop Received Ok");
				}
			}
			notbusy = true;
		}
		else if (button == backwardButton)
		{
			//code to send command to server
			 ftpClient.handleInput("query stop");
			
			if(cInfo.d_flag == true)
			{
				dWindow.addLine("Sent Stop Command");
			}
			if(Shared.roboInfo.connected == true && Shared.roboInfo.state.equals("stop"))
			{
				if(cInfo.d_flag == true)
				{
					dWindow.addLine("Command Stop Received Ok");
				}
			}
			notbusy = true;
		}
		else if (button == aboutButton)
		{
			if(cInfo.d_flag == true)
			{
				dWindow.addLine("Displaying About Box");
			}
			Aboutbox ab = new Aboutbox(mainframe);
			
		}
		else if (button == exitButton)
		{
			if(cInfo.d_flag == true)
			{
				dWindow.addLine("Disconnecting from server...");
			}
			ftpClient.handleInput("quit");
			if(cInfo.d_flag == true)
			{
				dWindow.addLine("Exiting....Good Bye");
			}
			System.exit(0);
		}
		else if (button == clawButton)
		{

			if(clawButton.getIcon() == clawopenicon_up)
			{
				clawButton.setIcon(clawcloseicon_up);
				clawButton.setPressedIcon(clawcloseicon_down);
				notbusy = true;
			}
			else if (clawButton.getIcon() == clawcloseicon_up)
			{
				clawButton.setIcon(clawopenicon_up);
				clawButton.setPressedIcon(clawopenicon_down);
				notbusy = true;
			}
		}
	}

	public void ConnLost()
	{
		forwardButton.setVisible(false);
		backwardButton.setVisible(false);
		leftButton.setVisible(false);
		rightButton.setVisible(false);
		clawButton.setVisible(false);
		connlostLabel.setVisible(true);
	}
	public void ConnRegained()
	{
		forwardButton.setVisible(true);
		backwardButton.setVisible(true);
		leftButton.setVisible(true);
		rightButton.setVisible(true);
		clawButton.setVisible(true);
		connlostLabel.setVisible(false);
	}

	@Override
	public void run() {
		while(true)
		{	
			if(notbusy == false)
			{
				while(notbusy == false)
				{
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			else if(notbusy)
			{
				if(notbusy)
				{
					ftpClient.handleInput("query checkconnect");
				}
				if(Shared.roboInfo.connected == false)
				{
					ConnLost();
					//this.invalidate();
					if(cInfo.d_flag == true)
					{
						dWindow.addLine("Connection to robot lost");
					}
					while(Shared.roboInfo.connected == false)
					{
						try {
							Thread.sleep(5000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						ftpClient.handleInput("query checkconnection");
					}
					if(cInfo.d_flag == true)
					{
						dWindow.addLine("Connection to robot regained");
					}
					ConnRegained();
				}
			
			}
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
